//
//  SettingViewController.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/17.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "SettingViewController.h"
#import "TypeDefineTableViewCell.h"
#import "CellTintModel.h"
#import "CellTintViewModel.h"
#import "EditInfomationVC.h"
#import "AccountBindVC.h"
@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *quiteBtn;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *sectionOneArr;
@property (nonatomic, strong) NSMutableArray *sectionTwoArr;
@property (nonatomic, strong) NSMutableArray *sectionThreeArr;

// 记录状态   是push  还是 pop  然后根据需求来调整导航栏
@property (nonatomic, assign) BOOL isPush;
@end


static NSString *typeOneCell = @"typeOneCell";
static NSString *typeTwoCell = @"typeTwoCell";
static NSString *typeThreeCell = @"typeThreeCell";
@implementation SettingViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.isPush = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (!self.isPush) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    self.view.backgroundColor = KLColor_C1;
    [self loadModel];
    [self loadTableView];
    [self loadQuiteButton];
}

- (void)loadModel{

    self.dataArray = [CellTintViewModel returnSettintData];
    for (int i = 0; i < self.dataArray.count; i++) {
        CellTintModel *model = self.dataArray[i];
        if (i >= 0 && i <= 1) {
            [self.sectionOneArr addObject:model];
        }else if (i > 1 &&i< 5){
            [self.sectionTwoArr addObject:model];
        }else{
            [self.sectionThreeArr addObject:model];
        }
    }
    
    
    
}

- (void)loadQuiteButton{
    [self.view addSubview:self.quiteBtn];
    [self.quiteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-15*HEIGHT);
        make.left.equalTo(self.view).offset(15*WIDTH);
        make.right.equalTo(self.view).offset(-15*WIDTH);
        make.height.mas_equalTo(44*HEIGHT);
        
    }];
}

// loadTableView
- (void)loadTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,KLScreenW,340*HEIGHT) style:UITableViewStyleGrouped];
    tableView.scrollEnabled = NO;
    tableView.backgroundColor = KLColor_C1;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.sectionFooterHeight = 10*HEIGHT;
    tableView.sectionHeaderHeight = 0;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, 10*HEIGHT)];
    tableView.tableHeaderView = headerView;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

// 退出登录
- (UIButton *)quiteBtn{
    if (!_quiteBtn) {
        _quiteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_quiteBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [_quiteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _quiteBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
        _quiteBtn.layer.masksToBounds = YES;
        _quiteBtn.layer.cornerRadius = 22*HEIGHT;
        _quiteBtn.backgroundColor = KLColor_A1;
        [[_quiteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            
        }];
    }
    return _quiteBtn;
}

#pragma mark  ---- tableView 的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.sectionOneArr.count;
    }else if (section == 1){
        return self.sectionTwoArr.count;
    }
    return self.sectionThreeArr.count;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        TypeDefineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:typeOneCell];
        if (!cell) {
            cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:typeOneCell type:DefineCellTypeInSecond];
        }
        CellTintModel *model = self.sectionOneArr[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        if (indexPath.row == self.sectionOneArr.count -1) {
            cell.isShowLine = YES;
        }
        return cell;
    }
    
    if (indexPath.section == 1) {
        TypeDefineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:typeTwoCell];
        if (indexPath.row == 2) {
            if (!cell) {
                cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:typeTwoCell type:DefineCellTypeSwitch];
            }
        }else{
            if (!cell) {
                cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:typeTwoCell type:DefineCellTypeInfoTint];
            }
            if (indexPath.row == 0) {
                cell.infoTint.text = @"96.45M";
            }else{
                cell.infoTint.text = @"每次提醒";
            }
        }
        CellTintModel *model = self.sectionTwoArr[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        if (indexPath.row == self.sectionTwoArr.count -1) {
            cell.isShowLine = YES;
        }
        
        return cell;
    }
    TypeDefineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:typeThreeCell];
    if (!cell) {
        cell = [[TypeDefineTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:typeThreeCell type:DefineCellTypeInfoTint];
    }
    CellTintModel *model = self.sectionThreeArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    if (indexPath.row == self.sectionThreeArr.count -1) {
        cell.isShowLine = YES;
    }
    cell.infoTint.text = APPVersion;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50*HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50*HEIGHT;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.isPush = YES;
    if (indexPath.section == 0 && indexPath.row == 0) {
        EditInfomationVC *editVc = [[EditInfomationVC alloc]init];
        [self.navigationController pushViewController:editVc animated:YES];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        AccountBindVC *accountVc = [[AccountBindVC alloc]init];
        [self.navigationController pushViewController:accountVc animated:YES];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KLScreenW, 10*HEIGHT)];
    bgView.backgroundColor = KLColor_C1;
    return bgView;
}


- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)sectionOneArr{
    if (!_sectionOneArr) {
        _sectionOneArr = [NSMutableArray array];
    }
    return _sectionOneArr;
}

- (NSMutableArray *)sectionTwoArr{
    if (!_sectionTwoArr) {
        _sectionTwoArr = [NSMutableArray array];
    }
    return _sectionTwoArr;
}

- (NSMutableArray *)sectionThreeArr{
    if (!_sectionThreeArr) {
        _sectionThreeArr = [NSMutableArray array];
    }
    return _sectionThreeArr;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
